/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import org.apache.log4j.Logger;

/**
 *
 * @author PhucAn
 */
public class QueryObject {

    private String table;
    private String columns;
    private String idColumn;
    private String sortColumns;
    private String sortDirection;
    private int currentPage;
    private int pageSize;
    private String whereClause;
    private int totals = 0;
    CallableStatement pc = null;
    Logger log=Logger.getLogger(QueryObject.class);
    

/**
     * 
     * @param table Table or View 's name
     * @param idColumn name of identifier column
     * @param sortColumns name of column use to sort
     */
    public QueryObject(String table, String idColumn, String sortColumns) {
        this.table = table;
        this.columns = "*";
        this.idColumn = idColumn;
        this.sortColumns = sortColumns;
        this.sortDirection = "asc";
        this.currentPage = 1;
        this.pageSize = 10;
        this.whereClause = "";

    }

    /**
     * @return the table or view's name
     */
    public String getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(String table) {
        this.table = table;
    }

    /**
     * @return the columns to select. Default is (*) - all columns. Column at index 1 is RowIndex value
     */
    public String getColumns() {
        return columns;
    }

    /**
     * @param specify columns will be selected
     */
    public void setColumns(String columns) {
        this.columns = columns;
    }

    /**
     * @return name of identifier column
     */
    public String getIdColumn() {
        return idColumn;
    }

    /**
     * @param set name of identifier column
     */
    public void setIdColumn(String idColumn) {
        this.idColumn = idColumn;
    }

    /**
     * @return the sort direction ( ASC | DESC). Default value is 'ASC'
     */
    public String getSortDirection() {
        return sortDirection;
    }

    /**
     * @param sortDirection set sort direction 
     */
    public void setSortDirection(String sortDirection) {
        this.sortDirection = sortDirection;
    }

    /**
     * @return the current page to query
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * @param set value of current page
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * @return get number of records per page
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * set number of records per page
     * @param  page size
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return the WHERE clause. Default value is empty
     */
    public String getWhereClause() {
        return whereClause;
    }

    /**
     * set WHERE clause 
     * @param the WHERE clause
     */
    public void setWhereClause(String whereClause) {
        this.whereClause = whereClause;
    }

    /**
     * 
     * @param conn - the connection
     * @return CallableStatement object 
     */
    public CallableStatement getCallableStatement(Connection conn) {

        try {

            pc = conn.prepareCall("{call getPage(?,?,?,?,?,?,?,?,?)}");
            pc.setString(1, table);
            pc.setString(2, columns);
            pc.setString(3, idColumn);
            pc.setString(4, sortColumns);
            pc.setString(5, sortDirection);
            pc.setInt(6, currentPage);
            pc.setInt(7, pageSize);
            pc.setString(8, whereClause);
            pc.setInt(9, totals);
            pc.registerOutParameter(9, Types.INTEGER);

        } catch (SQLException ex) {
            log.info("SQL Error: "+ex.getMessage());
        }catch(Exception ex){
            log.info("Error: "+ex.getMessage());
        }
        return pc;
    }

    /**
     * @return the column be used to sort
     */
    public String getSortColumns() {
        return sortColumns;
    }

    /**
     * @param  set columns be used to sort
     */
    public void setSortColumns(String sortColumns) {
        this.sortColumns = sortColumns;
    }

    /**
     * @return get totals records of the query
     */
    public int getTotals() {
        try {
            totals = pc.getInt(9);
        } catch (SQLException ex) {
          log.info("Error when trying to get value of out parameter: "+ex.getMessage());
        }
        return totals;
    }
    
   
}
